import tensorflow as tf
import numpy as np

#print(tf.__version__)

# 1. tf.Graph 1.1 tf.Operation  2. tf.Tensor
# 2. tf.Session

a = tf.constant(3.0, dtype=tf.float32)
b = tf.constant(4.0) # also tf.float32 implicitly
total = a + b
print(a)
print(b)
print(a + b)

# writer = tf.summary.FileWriter('.')
# writer.add_graph(tf.get_default_graph())
# writer.flush()

sess = tf.Session()
print(sess.run(total))
print(sess.run({'ab':(a,b), 'total':total}))

vec = tf.random_uniform(shape=(3,))
out1 = vec + 1
out2 = vec + 1
print(sess.run(vec))
print(sess.run(vec))
print(sess.run((out1, out2)))

x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
z = x + y
print(sess.run(z, feed_dict={x:3, y:4.5}))
print(sess.run(z, feed_dict={x:[1,3], y:[2, 4]}))

my_data = [[0, 1],
           [2,3],
           [4,5],
           [6,7]]
print('################### tf.data.Dataset###############')
slices = tf.data.Dataset.from_tensor_slices(my_data)
next_item = slices.make_one_shot_iterator().get_next()
while True:
    try:
        print(sess.run(next_item))
    except tf.errors.OutOfRangeError:
        break

print('############initialize iterator############')
r = tf.random_normal([10, 3])
dataset = tf.data.Dataset.from_tensor_slices(r)
iterator = dataset.make_initializable_iterator()
next_row = iterator.get_next()
sess.run(iterator.initializer)
while True:
    try:
        print(sess.run(next_row))
    except tf.errors.OutOfRangeError:
        break

print('#############linear model#############')
x = tf.placeholder(tf.float32, shape=[None, 3])
linear_model = tf.layers.Dense(units=1)
y = linear_model(x)

init = tf.global_variables_initializer()
sess.run(init)

print(sess.run(y, feed_dict={x:[[1,2,3], [4,5,6]]}))

print('################ tf.layers.dense')

x = tf.placeholder(tf.float32, shape=[None, 3])
y = tf.layers.dense(x, units=1)

init = tf.global_variables_initializer()
sess.run(init)
print(sess.run(y, feed_dict={x:[[1,2,3],[4,5,6]]}))

print('###########feature columns##########')
features = {'sales': [[5], [10], [8], [9]],
            'department':['sports', 'sports', 'gardening', 'gardening']}
department_column = tf.feature_column.categorical_column_with_vocabulary_list('department', ['sports', 'gardening'])
department_column = tf.feature_column.indicator_column(department_column)
columns = [tf.feature_column.numeric_column('sales'),
           department_column]
inputs = tf.feature_column.input_layer(features, columns)
var_init = tf.global_variables_initializer()
table_init = tf.tables_initializer()
sess.run((var_init, table_init))
print(sess.run(inputs))
